package name.marz.to.go.ahg.core;

import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;

public class AutoGetterHttpClient extends DefaultHttpClient {

	private String defaultCharset = "Shift_JIS";
	private static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko)";
	private static final String DEFAULT_COOKIE_POLICY = CookiePolicy.BEST_MATCH;

	public AutoGetterHttpClient() {
		super();
		init();
	}

	public AutoGetterHttpClient(ClientConnectionManager conman) {
		super(conman);
		init();
	}

	public AutoGetterHttpClient(HttpParams params) {
		super(params);
		init();
	}

	public AutoGetterHttpClient(ClientConnectionManager conman,
			HttpParams params) {
		super(conman, params);
		init();
	}
	
	private void init() {
		setCookiePolicy(DEFAULT_COOKIE_POLICY);
		setUserAgent(DEFAULT_USER_AGENT);
		// Set redirect strategy
		getParams().setParameter(ClientPNames.HANDLE_REDIRECTS, true);
		getParams().setBooleanParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);
		super.setRedirectStrategy(new AutoGetterRedirectStrategy());
	}

	public void setDefaultCharset(String defaultCharset) {
		this.defaultCharset = defaultCharset;
	}

	public String getDefaultCharset() {
		return defaultCharset;
	}

	public String getUserAgent() {
		return (String) getParams().getParameter(CoreProtocolPNames.USER_AGENT);
	}

	public void setUserAgent(String userAgent) {
		getParams().setParameter(CoreProtocolPNames.USER_AGENT, userAgent);
	}

	/**
	 * @return the cookiePolicy
	 */
	public String getCookiePolicy() {
		return (String) getParams().getParameter(ClientPNames.COOKIE_POLICY);
	}

	/**
	 * @param cookiePolicy the cookiePolicy to set
	 */
	public void setCookiePolicy(String cookiePolicy) {
		getParams().setParameter(ClientPNames.COOKIE_POLICY, cookiePolicy);
	}

}